package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.UUIDRandom;
import com.example.entity.*;
import com.example.dao.OrdersDao;
import com.example.service.OrdersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 订单表(Orders)表服务实现类
 *
 * @author makejava
 * @since 2022-05-28 11:56:06
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersDao,Orders> implements OrdersService {
    @Resource
    private OrdersDao ordersDao;
    @Resource
    private ShoppingCartServiceImpl shoppingCartService;
    @Resource
    private UserServiceImpl userService;
    @Resource
    private OrdersServiceImpl ordersService;
    @Resource
    private  AddressBookServiceImpl addressBookService;
    @Resource
    private OrderDetailServiceImpl orderDetailService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Orders queryById(Long id) {
        return this.ordersDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Orders> queryAllByLimit(int offset, int limit) {
        return this.ordersDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public Orders insert(Orders orders) {
        this.ordersDao.insert(orders);
        return orders;
    }

    /**
     * 修改数据
     *
     * @param orders 实例对象
     * @return 实例对象
     */
    @Override
    public Orders update(Orders orders) {
        this.ordersDao.update(orders);
        return this.queryById(orders.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.ordersDao.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean submit(Orders orders) {
        //获得当前用户id
        Long userid = orders.getUserId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userid);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper);
        Double all = 0.000;
        if (shoppingCarts.size() == 0 || shoppingCarts == null) {
            return false;
        }
        User user = userService.getById(userid);
        for (int i = 0; i < shoppingCarts.size(); i++) {
            all += shoppingCarts.get(i).getAmount() * shoppingCarts.get(i).getNumber();
        }
        orders.setAmount(all);
        orders.setNumber(UUIDRandom.getUUID());
        orders.setStatus(2);
        orders.setOrderTime(new Date());
        orders.setCheckoutTime(new Date());
        orders.setUserName(user.getName());
        orders.setPhone(user.getPhone());
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        boolean b = ordersService.save(orders);
        LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ShoppingCart::getUserId, userid);
        List<ShoppingCart> list1 = shoppingCartService.list(queryWrapper1);
        List<OrderDetail> list = new ArrayList<>();
        for (int i = 0; i < list1.size(); i++) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setDishFlavor(list1.get(i).getDishFlavor());
            orderDetail.setName(list1.get(i).getName());
            orderDetail.setImage(list1.get(i).getImage());
            orderDetail.setOrderId(orders.getId());
            if (list1.get(i).getDishId() != null) {
                orderDetail.setDishId(list1.get(i).getDishId());
            }
            if (list1.get(i).getSetmealId() != null) {
                orderDetail.setSetmealId(list1.get(i).getSetmealId());
            }
            orderDetail.setNumber(list1.get(i).getNumber());
            orderDetail.setAmount(list1.get(i).getAmount());
            list.add(orderDetail);
        }
        boolean a = orderDetailService.saveBatch(list);
            LambdaQueryWrapper<ShoppingCart> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(ShoppingCart::getUserId, userid);
            shoppingCartService.remove(queryWrapper2);
            return true;

    }
}
